using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using $solutionNameChild$.Core.MVC;
using $solutionNameChild$.Core.Tools;
using System.Text;
using System.Diagnostics;
using $solutionNameChild$.Core.Manager;

namespace $safeprojectname$.Controllers
{
    /// <summary>
    /// Controller that manage error
    /// </summary>
    public partial class ErrorController : BaseController
    {
        /// <summary>
        /// Display generic error page
        /// </summary>
        /// <returns>Generic error page</returns>
        public virtual ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Display Error 404 page
        /// </summary>
        /// <param name="ex">Exception</param>
        /// <param name="filePath">404 url</param>
        /// <returns>404 view</returns>
        public virtual ActionResult HttpError404(Exception ex, string filePath)
        {
            return View();
        }

        /// <summary>
        /// Write log for javascript error 
        /// </summary>
        /// <param name="description">error message</param>
        /// <param name="url">js file</param>
        /// <param name="line">line in error in js file</param>
        /// <param name="parent_url">url of the page</param>
        /// <param name="user_agent">user agent</param>
        public virtual void Log(string description, string url, string line, string parent_url, string user_agent)
        {
            StringBuilder stackTrace = new StringBuilder();
            stackTrace.Append("Url: " + url + "|");
            stackTrace.Append("Line: " + line + "|");
            stackTrace.Append("ParentUrl: " + parent_url + "|");
            if (User != null && User.Identity != null && !string.IsNullOrEmpty(User.Identity.Name) && User.Identity.IsAuthenticated)
            {
                stackTrace.Append("User: " + User.Identity.Name + "|");
            }
            stackTrace.Append("UserAgent: " + user_agent);
            IDictionary<string, object> properties = new Dictionary<string, object>();
            properties.Add("DateTime", DateTime.Now);
            properties.Add("DateTime Formated", DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
            properties.Add("StackTrace", stackTrace.ToString());
            properties.Add("NavigationHistories", NavigationHistories.Format());
            Logger.Write(description, "General", 1, 1, TraceEventType.Error, "Javascript Error", properties);
        }

    }
}
